<?php
/**

 * [Laike System] Copyright (c) 2017-2020 laiketui.com

 * Laike is not a free software, it under the license terms, visited http://www.laiketui.com/ for more details.

 */
require_once(MO_LIB_DIR . '/DBAction.class.php');

class addAction extends Action {

	public function getDefaultView() {
		$db = DBAction::getInstance();
		$request = $this->getContext()->getRequest();


        $cat_name = addslashes(trim($request->getParameter('cat_name'))); // 分类名称
        $sort = addslashes(trim($request->getParameter('sort'))); // 排序



		$request->setAttribute("cat_name",$cat_name);
		$request->setAttribute("sort",$sort);

		return View :: INPUT;
	}

	public function execute(){
		$db = DBAction::getInstance();
        $request = $this->getContext()->getRequest();
        $admin_id = $this->getContext()->getStorage()->read('admin_id');

        // 获取分类名称和排序号
        $cat_name = addslashes(trim($request->getParameter('cat_name'))); // 分类名称
        $sort = addslashes(trim($request->getParameter('sort'))); // 排序


        if($cat_name == ''){
            header("Content-type:text/html;charset=utf-8");
            echo "<script type='text/javascript'>" .
                "alert('中文名称不能为空！');" .
                "</script>";
            return $this->getDefaultView();
        }
		//检查分类名称是否重复
        $sql = "select * from lkt_article_class where cat_name = '$cat_name'";
  //       		var_dump($sql);
		// die();

		$r = $db->select($sql);
		// var_dump($r);
		// die();
		// 如果有数据 并且 数据条数大于0
        if ($r>0&& count($r)) {

	            header("Content-type:text/html;charset=utf-8");
	            echo "<script type='text/javascript'>" .
	                "alert('文章分类中文名称{$cat_name} 已经存在，请选用其他名称！');" .
	                "</script>";
	            return $this->getDefaultView();
	       
        }
		//添加分类
		$sql = "insert into lkt_article_class(cat_name,sort,add_date) "
            ."values('$cat_name','$sort',CURRENT_TIMESTAMP)";
			// var_dump($sql);
			// die();
		$r = $db->insert($sql);
		if($r == -1) {
            $db->admin_record($admin_id,'添加文章分类'.$cat_name.'失败',1);

            header("Content-type:text/html;charset=utf-8");
			echo "<script type='text/javascript'>" .
				"alert('未知原因，添加产品分类失败！');" .
				"</script>";
			return $this->getDefaultView();
		} else {
            $db->admin_record($admin_id,'添加文章分类'.$cat_name,1);

            header("Content-type:text/html;charset=utf-8");
			echo "<script type='text/javascript'>" .
				"alert('添加文章分类成功！');" .
				"location.href='index.php?module=article_class';</script>";
			return $this->getDefaultView();
		}
		
		return;
	}

	public function getRequestMethods(){
		return Request :: POST;
	}

	public function menu_merge($data,$parent_id=0,$level=1){
		$res = array();
		foreach ($data as $v) {
			if($v['sid']==$parent_id){
				$v['level'] = $level;
				$v['disabled'] = false;
				$res[]=$v;
				$res = array_merge($res,menu_merge($data,$v['id'],$level+1));
			}
		}
		return $res;
	}


}

?>